Location_();
var user_name = getCookie("user_name");
$('.user_name').text(user_name);

//网谈table
layui.use(['jquery', 'laydate', 'layer', 'table', 'form', 'element', 'laydate', 'upload'], function() {
	var element = layui.element;
	var table = layui.table;
	var form = layui.form;
	var $ = layui.$ //重点处
		,
		layer = layui.layer;
	var laydate = layui.laydate;
	laydate.render({
		elem: '#test6',
	});
	//线索添加表格
	table.render({
		elem: '#LAY_table_usering',
		size: 'sm',
		url: "http://" + com_url + "//admin/callDuration",
		response: {
			statusCode: 200
		},
		cols: [
			[{
				fixed: 'left',
				checkbox: true,
			}, {
				field: 'addtime',
				width: '24%',
				title: '日期'
			}, {
				field: 'bName',
				width: '24%',
				title: '部门',
			}, {
				field: 'name',
				width: '23%',
				title: '姓名'
			}, {
				toolbar: '#createTime',
				width: '24.5%',
				title: '通话时长'
			}]
		],
		page: true,
		limit: 10,
		id: 'adduser_table',
		done: function(res, count) { //回调函数
			var perm = res.perm;
			var code = res.code;
			if(code == 204) {
				clearCookie();
				window.parent.top.location.reload();
			}
		}
	});
	//	if(getPicTabNum) {
	//		$(".layui-tab-content>div").eq(getPicTabNum).addClass("layui-show").siblings().removeClass("layui-show");
	//	}
	//	if(getPicTabNum) {
	//		$('.ullis li').eq(getPicTabNum).trigger('click')
	//	}
	//	console.log(getPicTabNum)
	//	setTimeout(function(){
	//		$(".ullis li").eq(getPicTabNum).addClass("layui-this").siblings().removeClass("layui-this");
	//		
	//	},2000)
	//搜索功能
	var $ = layui.$,
		active = {
			reload_sbm: function() {
				var time = $('#test6').val();
				var department_ids = $('#seach_department').val();
				//执行重载
				table.reload('adduser_table', {
					page: {
						curr: 1 //重新从第 1 页开始
					},
					where: {
						time: time,
						department_id: department_ids
					}
				});
			},
			resoure_search: function() {
				var indexe = layer.load(1, {
					shade: [0.5, '#fff'] //0.1透明度的白色背景
				});

				var project_name = $('#project_name').val();
				$.ajax({
					type: "get",
					beforeSend: cookie_val_fun,
					url: "http://" + com_url + "//admin/summary",
					data: {
						name: project_name
					},
					success: function(res) {
						var data = JSON.parse(res);
						console.log(data)
						var data_arrys = data.data;
						var code = data.code;
						var msg = data.msg;
						if(code == "200") {
							layer.close(indexe);
							$('.panel_div').html("")
							render3(data_arrys)
						} else {
							layer.close(indexe)
							layer.msg(msg, {
								icon: 0,
								time: 3000
							});
						}
					},
				});
			},
			dbl_type: function() {
				function echart_(data, id, date, name, labels) {
					var option = {
						title: {
							text: name + "  达标率",
						},
						tooltip: {
							trigger: 'axis'
						},
						legend: {
							data: labels
						},
						toolbox: {
							show: true,
							feature: {
								dataZoom: {
									yAxisIndex: 'none'
								},
								dataView: {
									readOnly: false
								},
								magicType: {
									type: ['line', 'bar']
								},
								restore: {},
								saveAsImage: {}
							}
						},
						xAxis: {
							type: 'category',
							boundaryGap: false,
							data: date
						},
						yAxis: {
							type: 'value',
							axisLabel: {
								formatter: '{value}%'
							}
						},
						series: data
					};
					// 使用刚指定的配置项和数据显示图表。
					id.setOption(option);
				};

				//折线图
				$.ajax({
					type: "get",
					beforeSend: cookie_val_fun,
					url: "http://" + com_url + "//admin/resulSta",
					success: function(res) {
						var data = JSON.parse(res);
						var count = data.count;
						var date_ = data.data;
						var arrtest = [];
						var arr = Object.keys(count); 
						console.log(arr.length)
						$.each(count, function(index, curr) {
							// 基于准备好的dom，初始化echarts实例
							var labelVal = curr[1];
							var echart_demo = '<div class="mains" id="main' + index + '" ></div>';
							$('#echart_demos').append(echart_demo);
							if(arr.length>1){
								$('.mains').addClass('mainsMin');
							}else{
								$('.mains').addClass('mainsMax');
							}
							var myChart = echarts.init(document.getElementById('main' + index + ''));
							echart_(curr[0], myChart, date_, index, labelVal);
						});
					},
				});
			},
			depar_type: function() {
				var pal_arrys = [];
				$.ajax({
					type: "get",
					beforeSend: cookie_val_fun,
					url: "http://" + com_url + "//admin/summary",
					success: function(res) {
						var data = JSON.parse(res);
						var data_arry = data.data;
						render3(data_arry);
					},
				});

				function render3(data_arry) {
					$.each(data_arry, function(bigDepName, smallDeps) {
						var $bigDep = $('<div class="panel">' +
							'<div class="panel-heading panel_body  click' + bigDepName + '" style="background:#ccc3c3;cursor: pointer;">' +
							'<div class="row-fluid">' +
							'<span style="text-decoration: underline;color: #0320f8;" class="span2 ">' + bigDepName + '</span>' +
							'<span class="span2">项目名称</span>' +
							'<span class="span2">需求</span>' +
							'<span class="span2">已发资源</span>' +
							'<span class="span2">资源差额</span>' +
							'<span class="span2">ROI</span>' +
							'<span class="span2">达标率</span>' +
							'</div>' +
							'</div>' +
							'<div class="panel-headings panel_bodynr' + bigDepName + ' bigDepBody" style="display:none;"></div>' +
							'</div>');
						$('.panel_div').append($bigDep);
						$.each(smallDeps, function(smallDepName, smallDep) {
							var palHead = '';
							var palbody = '';
							$.each(smallDep.pal, function(key, val) {
								palHead += '<th>' + key + '</th>';
								palbody += '<td>' + val + '</td>';
							});
							var $smallDep = $('<div class="panel">' +
								'<div class="panel-heading panel_body small' + smallDep.depr + '" style="background:#f2f2f2;cursor: pointer;">' +
								'<div class="row-fluid">' +
								'<span class="span2" style="color:#006fff;text-decoration: underline;">' + smallDep.depr + '</span>' +
								'<span class="span2">' + smallDep.project + '</span>' +
								'<span class="span2">' + smallDep.allocation_number + '</span>' +
								'<span class="span2">' + smallDep.sum + '</span>' +
								'<span class="span2">' + smallDep.difference + '</span>' +
								'<span class="span2">' + smallDep.ROI + '</span>' +
								'<span class="span2">' + smallDep.compliance + '</span>' +
								'</div>' +
								'</div>' +
								'<div class="panel-headings panel' + smallDep.depr + ' smallDepBody"  style="display:none;"></div>' +
								'</div>');
							var $pal =
								'<table class="table table-hover table-condensed">' +
								'<thead>' +
								'<tr>' + palHead + ' </tr>' +
								'</thead>' +
								'<tbody>' +
								'<tr>' + palbody + ' </tr>' +
								'</tbody>' +
								'</table>';
							$smallDep.find('.smallDepBody').append($pal);
							$bigDep.find('.bigDepBody').append($smallDep);
							$('.small' + smallDep.depr + '').click(function() {
								$('.panel' + smallDep.depr + '').slideToggle({
									'slow': '30'
								});
							});
						});
						$('.click' + bigDepName + '').click(function() {
							$('.panel_bodynr' + bigDepName + '').slideToggle({
								'slow': '30'
							});
						});
					});
				}
			},
			Platform_type: function() {
				$.ajax({
					type: "get",
					beforeSend: cookie_val_fun,
					url: "http://" + com_url + "//admin/effectiveSta",
					success: function(res) {
						var data = JSON.parse(res);
						var obj_arry = data.data;
						console.log(obj_arry)
						render4(obj_arry)
					}
				});

				function render4(data_arry) {
					$.each(data_arry, function(bigDepName, smallDeps) {
						var $bigDep = $('<div class="panels" style="margin-top:10px;">' +
							'<div class="panel-heading panel_body  clicks' + bigDepName + '" style="background:#ccc3c3;cursor: pointer;">' +
							'<div class="row-fluid">' +
							'<span style="text-decoration: underline;color: #0320f8;" class="span2">' + bigDepName + '</span>' +
							'</div>' +
							'</div>' +
							'<div class="panel-headings panel_bodynr' + bigDepName + ' bigDepBody" style="display:none;"</div>' +
							'</div>');
						$('#Platform_demo').append($bigDep);
						$.each(smallDeps, function(smallDepName, smallDep) {
							var palHead = ""
							$.each(smallDep, function(key, val) {
								palHead += '<tr>' +
									'<td>' + key + '</td>' +
									'<td>' + val.eff + '</td>' +
									'<td>' + val.invalid + '</td>' +
									'<td>' + val.sum + '</td>' +
									'<td>' + val.rate + '</td>' +
									'</tr>';
							});
							var $pal =
								'<table class="table table-hover table-condensed">' +
								'<thead>' +
								'<tr>' +
								'<th>平台名称</th>' +
								'<th>有效</th>' +
								'<th>无效</th>' +
								'<th>总数</th>' +
								'<th>有效率</th>' +
								'</tr>' +
								'</thead>' +
								'<tbody>' + palHead + '</tbody>' +
								'</table>';
							var $smallDep = $('<div class="panels" style="border:1px solid gainsboro;margin-top:10px;">' +
								'<div class="panel-heading panel_body small' + smallDepName + '" style="background:#f2f2f2;cursor: pointer;">' +
								'<div class="row-fluid">' +
								'<span class="span2" style="color:#006fff;text-decoration: underline;">' + smallDepName + '</span>' +
								'</div>' +
								'</div>' +
								'<div class="panel-headings panelsdd' + smallDepName + '  smallDepBody" style="display:none;"></div>' +
								'</div>');
							$bigDep.find('.bigDepBody').append($smallDep);
							$smallDep.find('.smallDepBody').append($pal);
							$('.small' + smallDepName + '').click(function() {
								$('.panelsdd' + smallDepName + '').slideToggle({
									'slow': '30'
								});
							});
						});
						$('.clicks' + bigDepName + '').click(function() {
							$('.panel_bodynr' + bigDepName + '').slideToggle({
								'slow': '30'
							});
						});
					});
				}
			}

		};

	$('.searchs,.depart_tj,.dbl_class,.Platform_efficiency').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
		$('.panel_div,#Platform_demo').html("")
	});

	$(".ullis li").eq(parseInt(getPicTabNum) + 1).addClass("layui-this").siblings().removeClass("layui-this");
	$('.ullis li').eq(parseInt(getPicTabNum) + 1).trigger('click');
	//监听单元格事件
	table.on('tool(leader)', function(obj) {
		var data = obj.data;
		var datas = JSON.parse(JSON.stringify(data))
		if(obj.event === 'setSign') {
			role_id = datas.role_id;
			var position_name = datas.name;
			user_names = datas.name;
			$('#owner_role_id').val(role_id);
		}
	});

	//	$.ajax({
	//		type: "get",
	//		beforeSend: cookie_val_fun,
	//		url: "http://" + com_url + "//admin/getBranchList",
	//		success: function(res) {
	//			var data = JSON.parse(res);
	//			var department_list = data.data;
	//			var code = data.code;
	//			let msg = data.msg;
	//			if(code == "200") {
	//				//渲染部门
	//				var add_user_department = $(".add_user_department");
	//				add_user_department.append($("<option></option>").attr("value", '').text('--请选择--'));
	//				$.each(department_list, function(key, role) { //遍历  
	//					add_user_department.append($("<option ></option>").attr("value", role.department_id).text(role.name));
	//					form.render('select')
	//				});
	//			} else {
	//				layer.msg(msg, {
	//					icon: 0
	//				})
	//			}
	//		},
	//	});

});